import { FairyGUI, FairyEditor } from 'csharp';

const App = FairyEditor.App;

App.pluginManager.LoadUIPackage(App.pluginManager.basePath + "/" + eval("__dirname") + '/CocosInspector');

class CocosInspector extends FairyEditor.View.PluginInspector {
    private combo: FairyGUI.GComboBox;

    public constructor() {
        super();

        this.panel = FairyGUI.UIPackage.CreateObject("CocosInspector", "WindowType").asCom;
        this.combo = this.panel.GetChild("windowType").asComboBox;
        this.combo.onChanged.Add(() => {
            let obj = App.activeDoc.inspectingTarget;

            console.log(`改变选择`);
            console.log(obj);
            console.log(obj.docElement);
            console.log(this.combo);
            console.log(this.combo.value);
            //use obj.docElement.SetProperty('xxx',..) instead of obj.xxx = ... to enable undo/redo mechanism
            try {
                obj.docElement.SetProperty("remark", this.combo.value);
            } catch (error) {
                console.log(error);
            }
        });

        this.updateAction = () => { return this.updateUI(); };
    }

    private updateUI(): boolean {
        let sels = App.activeDoc.inspectingTargets
        let obj = sels.get_Item(0);

        this.combo.value = obj["remark"];

        return true; //if everything is ok, return false to hide the inspector
    }
}

//Register a inspector
App.inspectorView.AddInspector(() => new CocosInspector(), "CocosInspector", "CocosInspector");
//Condition to show it
App.docFactory.ConnectInspector("CocosInspector", "mixed", true, false);